package cn.com.nes.site.service.crm.opportunity.impl;

import cn.com.nes.common.em.DictTypeEnum;
import cn.com.nes.common.exception.MissBusinessParameterException;
import cn.com.nes.common.util.OssUtil;
import cn.com.nes.mybatis.agent.crm.entity.OpportunityFile;
import cn.com.nes.mybatis.agent.crm.mapper.OpportunityFileMapper;
import cn.com.nes.site.entity.em.crm.log.LogOperateTypeEnum;
import cn.com.nes.site.entity.em.crm.opportunity.OpportunityFileTypeEnum;
import cn.com.nes.site.entity.em.crm.opportunity.OpportunityStageEnum;
import cn.com.nes.site.service.crm.log.LogOperateService;
import cn.com.nes.site.service.crm.log.bo.LogOperateBO;
import cn.com.nes.site.service.crm.opportunity.CrmOpportunityService;
import cn.com.nes.site.service.crm.opportunity.OpportunityFileService;
import cn.com.nes.site.service.crm.opportunity.bo.DelOpportunityFileBO;
import cn.com.nes.site.service.crm.opportunity.bo.OpportunityFileBO;
import cn.com.nes.site.service.crm.opportunity.bo.SaveOpportunityFileBO;
import cn.com.nes.site.service.crm.opportunity.dto.OpportunityFileDto;
import cn.com.nes.site.service.system.dict.TParamService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

@Service
public class OpportunityFileServiceImpl extends ServiceImpl<OpportunityFileMapper, OpportunityFile> implements OpportunityFileService {

    @Resource
    private CrmOpportunityService crmOpportunityService;

    @Resource
    private LogOperateService logOperateService;

    @Resource
    private TParamService tParamService;

    @Override
    public Page<OpportunityFileDto> getOpportunityFilePage(OpportunityFileBO opportunityFileBo) {
        if (StrUtil.isEmpty(opportunityFileBo.getOpportunityId())) {
            throw new MissBusinessParameterException("商机Id");
        }
        Page<OpportunityFileDto> page = new Page<>(opportunityFileBo.getPageid(), opportunityFileBo.getPagesize());
        List<OpportunityFileDto> opportunityFileDtoList = this.baseMapper.getOpportunityFileDtoList(page, opportunityFileBo);

        //获取商机文件真实路径
        Iterator<OpportunityFileDto> iterator = opportunityFileDtoList.iterator();
        while (iterator.hasNext()) {
            OpportunityFileDto next = iterator.next();
            String filepath = next.getFilePath();
            String imgSignURL = OssUtil.getFileSignURL(filepath, 0);
            next.setRealFilePath(imgSignURL);
            setOpportunityFileFields(next);
        }
        page.setRecords(opportunityFileDtoList);
        return page;
    }

    private void setOpportunityFileFields(OpportunityFileDto opportunityFileDto) {
        opportunityFileDto.setFileTypeName(tParamService.getNameByParamTypeAndValue(DictTypeEnum.OPPORTUNITYFILETYPE.getParamType(), opportunityFileDto.getFileType()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OpportunityFile saveOpportunityFileFunction(SaveOpportunityFileBO bo) throws Exception {
        //修改商机阶段
        crmOpportunityService.handleOpportunityStage(bo.getOpportunityUuid(), OpportunityStageEnum._follow_up, bo.getOperateUserId(), bo.getOperateDate(), bo.getOperateUserName(), bo.getOperateUserCompanyId());

        return saveOpportunityFile(bo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OpportunityFile saveOpportunityFile(SaveOpportunityFileBO bo) {
        OpportunityFile opportunityFile = new OpportunityFile();
        BeanUtils.copyProperties(bo, opportunityFile);
        opportunityFile.setOpportunityId(bo.getOpportunityUuid());
        if (StrUtil.isBlank(opportunityFile.getUuid())) {
            opportunityFile.setUuid(UUID.randomUUID().toString());
            opportunityFile.setCreateTime(bo.getOperateDate());
            opportunityFile.setCreateUid(String.valueOf(bo.getOperateUserId()));
            opportunityFile.setDeleted("0");
        }
        this.saveOrUpdate(opportunityFile);

        operateLog(bo);
        return opportunityFile;
    }

    private void operateLog(SaveOpportunityFileBO bo) {
        // 记录操作日志
        LogOperateBO logOperateBO = new LogOperateBO();
        logOperateBO.setLogType(LogOperateTypeEnum.OPPORTUNITY);
        logOperateBO.setOpportunityUuid(bo.getOpportunityUuid());
        logOperateBO.setOperateTime(bo.getOperateDate());
        logOperateBO.setOperateUserId(bo.getOperateUserId());
        logOperateBO.setRemark("上传文件：" + OpportunityFileTypeEnum.getEnum(bo.getFileType())
                .getValue() + "-" + bo.getFileName());
        logOperateService.saveLogOperate(logOperateBO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOpportunityFile(List<SaveOpportunityFileBO> fileBos) {
        List<OpportunityFile> files = new ArrayList<>();
        for (SaveOpportunityFileBO bo : fileBos) {
            OpportunityFile opportunityFile = new OpportunityFile();
            BeanUtils.copyProperties(bo, opportunityFile);
            opportunityFile.setOpportunityId(bo.getOpportunityUuid());
            if (StrUtil.isBlank(opportunityFile.getUuid())) {
                opportunityFile.setUuid(UUID.randomUUID().toString());
                opportunityFile.setCreateTime(bo.getOperateDate());
                opportunityFile.setCreateUid(String.valueOf(bo.getOperateUserId()));
                opportunityFile.setDeleted("0");
            }
            files.add(opportunityFile);
        }

        this.saveBatch(files);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public OpportunityFile delOpportunityFile(DelOpportunityFileBO bo) throws Exception {
        OpportunityFile opportunityFile = new OpportunityFile();
        opportunityFile.setUuid(bo.getOpportunityFileUuid());
        opportunityFile.setDeleted("1");
        this.saveOrUpdate(opportunityFile);
        return opportunityFile;
    }

    @Override
    public List<OpportunityFileDto> getOpportunityFileList(String opportunityUuid, List<String> opportunityFileTypeList) {
        List<OpportunityFileDto> opportunityFileDtoList = this.baseMapper.getFileListByOpportunityId(opportunityUuid, opportunityFileTypeList);

        //获取商机文件真实路径
        Iterator<OpportunityFileDto> iterator = opportunityFileDtoList.iterator();
        while (iterator.hasNext()) {
            OpportunityFileDto next = iterator.next();
            String filepath = next.getFilePath();
            String imgSignURL = OssUtil.getFileSignURL(filepath, 0);
            next.setRealFilePath(imgSignURL);
        }
        return opportunityFileDtoList;
    }

}
